package com.tangmu.rabbits.ui.activity;

import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.base.BaseActivity;
import com.tangmu.rabbits.ui.fragment.home.HomeStickyFragment;
import com.tangmu.rabbits.ui.fragment.mine.MineFragment;
import com.tangmu.rabbits.ui.fragment.optimization.OptimizationFragment;
import com.tangmu.rabbits.ui.fragment.search.SearchFragment;
import com.tangmu.rabbits.ui.fragment.ticket.TicketFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.layout_container)
    FrameLayout layoutContainer;
    @BindView(R.id.rb_main_home)
    RadioButton rbMainHome;
    @BindView(R.id.rb_main_optimization)
    RadioButton rbMainOptimization;
    @BindView(R.id.rb_main_search)
    RadioButton rbMainSearch;
    @BindView(R.id.rb_main_ticket)
    RadioButton rbMainTicket;
    @BindView(R.id.rb_main_mine)
    RadioButton rbMainMine;
    @BindView(R.id.rg_main_group)
    RadioGroup rgMainGroup;
    //private HomeFragment homeFragment;//
    private HomeStickyFragment homeFragment;
    private OptimizationFragment optimizationFragment;
    private SearchFragment searchFragment;
    private TicketFragment ticketFragment;
    private MineFragment mineFragment;

    private FragmentTransaction ft;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        setSwitchFragment(0);

        rgMainGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_main_home:
                        setSwitchFragment(0);
                        break;
                    case R.id.rb_main_optimization:
                        setSwitchFragment(1);
                        break;
                    case R.id.rb_main_search:
                        setSwitchFragment(2);
                        break;
                    case R.id.rb_main_ticket:
                        setSwitchFragment(3);
                        break;
                    case R.id.rb_main_mine:
                        setSwitchFragment(4);
                        break;
                }
            }
        });
    }

    private void setSwitchFragment(int i) {
        ft = getSupportFragmentManager().beginTransaction();
        hideFragments(ft);
        switch (i) {
            case 0:
                homeFragment = (HomeStickyFragment) getSupportFragmentManager().findFragmentByTag(HomeStickyFragment.TAG);
                if (homeFragment == null) {
                    homeFragment = new HomeStickyFragment();
                    ft.add(R.id.layout_container, homeFragment, HomeStickyFragment.TAG);
                } else {
                    ft.show(homeFragment);
                }
                break;
            case 1:
                optimizationFragment = (OptimizationFragment) getSupportFragmentManager().findFragmentByTag(OptimizationFragment.TAG);
                if (optimizationFragment == null) {
                    optimizationFragment = new OptimizationFragment();
                    ft.add(R.id.layout_container, optimizationFragment, OptimizationFragment.TAG);
                } else {
                    ft.show(optimizationFragment);
                }
                break;
            case 2:
                searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag(SearchFragment.TAG);
                if (searchFragment == null) {
                    searchFragment = new SearchFragment();
                    ft.add(R.id.layout_container, searchFragment, SearchFragment.TAG);
                } else {
                    ft.show(searchFragment);
                }
                break;
            case 3:
                ticketFragment = (TicketFragment) getSupportFragmentManager().findFragmentByTag(TicketFragment.TAG);
                if (ticketFragment == null) {
                    ticketFragment = new TicketFragment();
                    ft.add(R.id.layout_container, ticketFragment, TicketFragment.TAG);
                } else {
                    ft.show(ticketFragment);
                }
                break;
            case 4:
                mineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag(MineFragment.TAG);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.layout_container, mineFragment, MineFragment.TAG);
                } else {
                    ft.show(mineFragment);
                }
                break;
        }
        ft.commit();
    }

    private void hideFragments(FragmentTransaction ft) {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (optimizationFragment != null) {
            ft.hide(optimizationFragment);
        }

        if (searchFragment != null) {
            ft.hide(searchFragment);
        }
        if (ticketFragment != null) {
            ft.hide(ticketFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }
    }
}
